]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/Tests/Unit Tests/HelperFunctionTest.m
mDNSResponder-1310.40.42.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / Tests / Unit Tests / HelperFunctionTest.m
1 //
2 // HelperFunctionTest.m
3 // Tests
4 //
5 // Copyright (c) 2019 Apple Inc. All rights reserved.
6 //
7
8 #import <XCTest/XCTest.h>
9 #include "unittest_common.h"
10 #include "helper.h"
11
12 @interface HelperFunctionTest : XCTestCase
13
14 @end
15
16 @implementation HelperFunctionTest
17
18 - (void)setUp {
19 // It is empty for now.
20 }
21
22 - (void)tearDown {
23 // It is empty for now.
24 }
25
26 - (void)testCFStringToDomainLabel {
27 // test_cstring[i][0] is the input
28 // test_cstring[i][1] is the expected correct output
29 static const char * const test_cstring[][2] = {
30 {"short", "short"},
31 {"this-is-a-normal-computer-name", "this-is-a-normal-computer-name"},
32 {"", ""},
33 {"This is an ascii string whose length is more than 63 bytes, where it takes one byte to store every character", "This is an ascii string whose length is more than 63 bytes, whe"},
34 {"यह एक एस्सी स्ट्रिंग है जिसकी लंबाई साठ तीन बाइट्स से अधिक है, जहां यह हर चरित्र को संग्रहीत करने के लिए एक बाइट लेता है", "यह एक एस्सी स्ट्रिंग है "}, // "यह एक एस्सी स्ट्रिंग है " is 62 byte, and "यह एक एस्सी स्ट्रिंग है जि" is more than 63, so the result is expected to truncated to 62 bytes instead of 63 bytes
35 {"वितीय टेस्ट ट्राई टी॰वी॰", "वितीय टेस्ट ट्राई टी॰वी"},
36 {"这是一个超过六十三比特的其中每个中文字符占三比特的中文字符串", "这是一个超过六十三比特的其中每个中文字符占"},
37 {"🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝", "🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝"} // "🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝" is 60 bytes, and "🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝🃝" is more than 63 bytes so the result is expected to be truncated to 60 bytes instead of 64 bytes
38 };
39
40 for (int i = 0, n = sizeof(test_cstring) / sizeof(test_cstring[0]); i < n; i++) {
41 // construct CFString from input
42 CFStringRef name_ref = CFStringCreateWithCString(kCFAllocatorDefault, test_cstring[i][0], kCFStringEncodingUTF8);
43 XCTAssertTrue(name_ref != NULL, @"unit test internal error. {descrption=\"name_ref should be non-NULL.\"}");
44
45 // call the function being tested
46 domainlabel label;
47 mDNSDomainLabelFromCFString_ut(name_ref, &label);
48
49 // Check if the result is correct
50 XCTAssertEqual(label.c[0], strlen(test_cstring[i][1]),
51 @"name length is not equal. {expect=%d,actual=%d}", strlen(test_cstring[i][1]), label.c[0]);
52 XCTAssertTrue(memcmp(label.c + 1, test_cstring[i][1], label.c[0]) == 0,
53 @"name is not correctly decoded. {expect='%s',actual='%s'}", test_cstring[i][1], label.c + 1);
54
55 CFRelease(name_ref);
56 }
57 }
58
59 - (void)testHelperRequestBPF
60 {
61 fprintf(stdout, "Start %s\n", __FUNCTION__);
62 mDNSRequestBPF();
63 fprintf(stdout, "Completed %s\n", __FUNCTION__);
64 }
65
66 @end